package com.page.conf;

//import java.util.ArrayList;
//import java.util.List;
//import java.util.Vector;

//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;

import com.page.common.util.PropertyConfig;
//import com.page.common.util.Tokens;
import com.page.common.vo.HostVO;
//import com.page.common.vo.TokenVO;
import com.page.exception.ExceptionUtil;
import com.page.exception.Exception;

/**
 * 配置文件处理类
 * @Package com.page.conf
 * @File Config.java
 * @author shuxin.zheng@net263.com
 * @date 2014-9-17 下午01:40:03
 */
public class Config {
/*
    private static boolean isWindows = false;
    static {
        String os = System.getProperties().getProperty("os.name");
        if (os != null && os.toUpperCase().indexOf("WINDOWS") != -1)
            isWindows = true;
    }
*/   
    //private static final Logger log = LoggerFactory
    //        .getLogger(Config.class);
    //private List<HostVO> servers = new ArrayList<HostVO>();
    private int connectTimeout = 5000;
    private int soTimeout = 5000;
    private HostVO manager;
    private String url;

    /**
     * 从配置文件中读取配置选项
     */
    private Config() {
        String value = PropertyConfig.getPropertyValue("connectTimeout");
        if (value != null)
            connectTimeout = Integer.parseInt(value);
        value = PropertyConfig.getPropertyValue("soTimeout");
        if (value != null)
            soTimeout = Integer.parseInt(value);

        /**
         * for servers
         */
        
        /*
        // 获取服务器地址列表，格式：ip:port, ip:port, ...
        value = PropertyConfig.getPropertyValue("servers.addrs").trim();
        if (value != null) {
            Tokens tokens = new Tokens(value);
            Vector<TokenVO> v = tokens.getTokens();
            for (TokenVO vo : v) {
                if (vo.size() < 2)
                    continue;

                String ip = vo.getFirst();
                int port = Integer.parseInt(vo.get(1));
                if (port <= 0) {
                    log.error("invalid port: " + port + "; ip: " + ip);
                    continue;
                }
                HostVO host = new HostVO(ip, port);
                host.setConnectTimeout(connectTimeout).setSoTimeout(soTimeout);
                servers.add(host);
            }
        }
        if (servers.size() == 0) {
            log.error("no abcs addr available!");
            throw new Exception(ExceptionUtil.NO_HOST,
                    "no available host");
        }
        */
        
        // 管理服务器地址配置项
        String host = PropertyConfig.getPropertyValue("manager.host").trim();
        if (host == null)
            throw new Exception(ExceptionUtil.NO_HOST,
                    "no manager host");
        String port = PropertyConfig.getPropertyValue("manager.port").trim();
        if (port == null)
            throw new Exception(ExceptionUtil.NO_PORT,
                    "no manager port");
        manager = new HostVO(host, Integer.parseInt(port));
        manager.setConnectTimeout(connectTimeout).setSoTimeout(soTimeout);
        
        // URL 配置项
        url = PropertyConfig.getPropertyValue("url");
        if (url == null)
            throw new Exception(ExceptionUtil.NO_URL, "no url");
    }
    
    /**
     * 获得网络 IO 读写超时时间
     * 
     * @return {int} 网络 IO 读写超时时间（毫秒）
     */
    public int getSoTimeout() {
        return soTimeout;
    }

    /**
     * 获得网络连接超时时间
     * 
     * @return {int} 连接服务器超时时间（毫秒）
     */
    public int getConnectTimeout() {
        return connectTimeout;
    }
    
    /**
     * 获得所有服务器的地址（不含管理服务器）
     * @return {List<HostVO>}
     */
    /*
    public List<HostVO> getServers() {
        return servers;
    }
    */
    
    /**
     * 获得管理服务器地址
     * @return {HostVO}
     */
    public HostVO getManager() {
        return manager;
    }
    
    /**
     * 获得请求的 URL 配置项
     * @return {String}
     */
    public String getUrl() {
        return url;
    }
    
    //////////////////////////////////////////////////////////////////////////
    
    private static class SingletonHolder {
        private static Config instance = new Config();
    }

    /**
     * 获得配置对象的单例
     * 
     * @return {Config}
     */
    public static Config getInstance() {
        return SingletonHolder.instance;
    }

    // ///////////////////////////////////////////////////////////////////////

    public static void main(String[] args) {
        int soTimeout = Config.getInstance().getSoTimeout();
        System.out.println("soTimeout: " + soTimeout);
    }

}
